<?php
namespace app\console\controller;
use app\console\model\Privilege as SelfModel;
use think\facade\Request;
use think\facade\Route;
use think\facade\View;

class Privilege extends Base
{


    public function index()
    {
        $data = SelfModel::order(['sort' => 'asc','id' => 'asc'])->select()->toArray();
        // echo Db::getlastSql();
        $data = getMenuTree($data);
//        foreach ($data as $k => $v){
//            echo str_repeat('-',$v['level']).$v['cat_name'].'<br/>';
//        }
        //halt(collection($data)->toArray());
        $view = [
            'title' => '商品分类列表',
            'en_title' => 'WebCategory list',
            'create_title' => '添加商品分类',
            'description' => '本列表为商品分类列表，添加商品时必添加选项列，请把需要的分类添加好，否则商品会因为无对应分类而无法添加'
        ];
        file_put_contents('privilege.php',var_export($data,true));
        return View::fetch('', ['data' => $data, 'view' => $view]);
    }

    public function create()
    {

        if ($this->request->isPost()) {
            $param = $this->request->param();

            $ret = (new SelfModel())->save($param);
            if ($ret) {
                win('操作成功', Route::buildUrl('index'));
            }
            fail('操作失败');
        }

        $list = getMenuTree(SelfModel::order(['sort' => 'asc','id' => 'asc'])->select());
        return View::fetch('', ['list' => $list,'info' => []]);
    }



    public function update()
    {
        $param = $this->request->param();
        $id = input('id');
        $info = SelfModel::find($id);
        if ($this->request->isPost()) {

            $ret = $info->save($param, $id);
            if ($ret !== false) {
                win('修改成功', Route::buildUrl('index'));
            }
            fail('修改失败');
        }
        $list = getMenuTree(SelfModel::order(['sort' => 'asc','id' => 'asc'])->where('id','>',90)->select());
        $lower_level = sonPath(SelfModel::select(),$id);
        $lower_level[] = $id;

        return View::fetch('create', ['list' => $list, 'info' => $info,'lower_level' => $lower_level]);
    }

    public function destory($id)
    {
        if ($this->request->isGet()) {
            $ret = SelfModel::destroy($id);
            if ($ret) jump('删除成功', Route::buildUrl('index'));
            fail('删除失败');
        }
    }

    public function check()
    {
        $inquire = $this->request->param();
        $ret = SelfModel::where('id', $inquire['id'])->value($inquire['check']) == '1' ? 2 : 1;
        if (SelfModel::where('id', $inquire['id'])->update([$inquire['check'] => $ret])) {
            return $ret;
        }
        return false;
    }

    /**
     * 首页批量更新
     */
    public function index_update()
    {
        $param = $this->request->param();
        $data = [];
        if(!empty($param['sort'])){
            foreach ($param['sort'] as $k => $v) {
                $data[$k]['id'] = $k;
                $data[$k]['sort'] = $v;
                // $ret = Db::name($this->controller)->where('id',$k)->setField('sort',$v);
            }

        }
        $ret = (new SelfModel())->allowField(['id','sort'])->saveAll($data);
        if ($ret !== false) {
            jump('更新成功', Route::buildUrl('index'));
        }
    }

    /**
     * 批量子集
     */
    public function delete_son($id)
    {
        $args_where =  sonPath(SelfModel::select(),$id,true);
        $ret = SelfModel::destroy($args_where);
        if($ret){
            return json(['code'=>200,'message'=>'成功','ret'=>$ret]);
        }
        return json(['code'=>400,'message'=>'失败']);

    }


}